package uutalk.netcloud.talk.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.Constants;
import uutalk.netcloud.talk.app.ui.ContentActivity;
import uutalk.netcloud.talk.base.util.thirdpart.ImageLoadListener;
import uutalk.netcloud.talk.base.util.thirdpart.ImageLoader;
import uutalk.netcloud.talk.base.util.thirdpart.QiNiuManager;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.model.bean.GroupMember;

/**
 * Created by Administrator on 2017/12/14.
 */

public class GroupMemberAdapter extends RecyclerView.Adapter<GroupMemberAdapter.GroupMemberHolder> {

    private Context context;
    List<GroupMember> groupMemberList;

    public GroupMemberAdapter(Context context, List<GroupMember> groupMemberList){

        if(groupMemberList == null){
            this.groupMemberList = new ArrayList<>();
        } else {
            this.groupMemberList = groupMemberList;
        }
        this.context = context;

    }

    @Override
    public GroupMemberHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_member, parent, false);
        return new GroupMemberHolder(view);
    }

    @Override
    public void onBindViewHolder(final GroupMemberHolder holder, int position) {
        final GroupMember groupMember = groupMemberList.get(position);
        ImageLoader.loadImageBitmap(context, QiNiuManager.QINIU_HOST + groupMember.getUser_picture(), new ImageLoadListener() {
            @Override
            public void onLoadComplete(Object t) {
                holder.rivIcon.setImageBitmap((Bitmap)t);
            }

            @Override
            public void onLoadFail() {

            }
        });
        holder.tvName.setText(groupMember.getUser_name());
        if(!"1".equals(groupMember.getUser_status())){
            holder.tvName.setTextColor(context.getResources().getColor(R.color.text_gray));
        } else {
            holder.tvName.setTextColor(context.getResources().getColor(R.color.text_black));
        }

        if("1".equals(groupMember.getUser_type())){
            holder.tvGroupType.setVisibility(View.VISIBLE);
        } else {
            holder.tvGroupType.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("GroupMemberAdapter","id="+groupMember.getUser_id());
                if (groupMember.getUser_id().equals(UserManager.getInstance().getUserInfo().getUser_id())){
                    //fixme
//                    Intent intent = new Intent(context, UserInfoActivity.class);
//                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, ContentActivity.class);
                    intent.putExtra(Constants.KEY_FRAGMENT, Constants.USER_INFO_FRAGMENT);
                    intent.putExtra(Constants.KEY_UID, groupMember.getUser_id());
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return groupMemberList.size();
    }

    public static class GroupMemberHolder extends RecyclerView.ViewHolder{
        public RoundedImageView rivIcon;
        public TextView tvName, tvGroupType;

        public GroupMemberHolder(View itemView) {
            super(itemView);
            rivIcon = (RoundedImageView) itemView.findViewById(R.id.headIV);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvGroupType = (TextView) itemView.findViewById(R.id.tvGroupType);
        }
    }

    /**
     * 将彩色图转换为纯黑白二色
     *
     * @param
     * @return 返回转换好的位图
     */
    private Bitmap convertToBlackWhite(Bitmap bmp) {
        int width = bmp.getWidth(); // 获取位图的宽
        int height = bmp.getHeight(); // 获取位图的高
        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

        bmp.getPixels(pixels, 0, width, 0, 0, width, height);
        int alpha = 0xFF << 24;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];

                // 分离三原色
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);

                // 转化成灰度像素
                grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
                grey = alpha | (grey << 16) | (grey << 8) | grey;
                pixels[width * i + j] = grey;
            }
        }

        // 新建图片
        Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        // 设置图片数据
        newBmp.setPixels(pixels, 0, width, 0, 0, width, height);

        Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
        return resizeBmp;
    }
}
